﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace GACore.InputArgs
{
	public class Persistence
	{
		public Preferences Load(string filename)
		{
			XmlSerializer x = new XmlSerializer(typeof(Preferences));
			try
			{
				FileStream stream = new FileStream(filename, FileMode.Open);
				return (Preferences)x.Deserialize(stream);
			}
			catch (Exception)
			{
				throw new FileLoadException("Die angegebene Datei beschreibt keine gültige Einstellung für den genetischen Algorithmus.");
			}
		}

		public void Persist(string filename, Preferences preferences)
		{
			XmlSerializer x = new XmlSerializer(typeof(Preferences));
			StreamWriter writer = new StreamWriter(filename, false);
			x.Serialize(writer, preferences);
			writer.Close();
		}

	}
}
